package com.jhx.video.widget

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.jhx.video.R

/**
 * 回复评论弹窗
 *
 * @author Jack 2018-03-28 14:59
 */
class ReplyDialog(context: Context) : Dialog(context, R.style.dialog_bottom_full) {
    var onSendClick: ((msg: String?) -> Unit)? = null

    init {
        setContentView(R.layout.dialog_reply)
        val layoutParams = window.attributes
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT

        layoutParams.gravity = Gravity.BOTTOM
        window.attributes = layoutParams
        val closeTxt  = findViewById<TextView>(R.id.txt_close)
        val replyEdit = findViewById<EditText>(R.id.edit_reply)
        val sendTxt   = findViewById<TextView>(R.id.txt_send)
        val numTxt    = findViewById<TextView>(R.id.txt_num)
        setOnShowListener {
            numTxt.text = "0/60"
            replyEdit.text.clear()
            closeTxt.postDelayed({
                showKeyboard(replyEdit)
            }, 300)
        }
        setOnDismissListener {
            hideKeyboard(replyEdit)
        }
        closeTxt.setOnClickListener {
            dismiss()
        }
        replyEdit.addTextChangedListener(object: TextWatcher {
            @SuppressLint("SetTextI18n")
            override fun afterTextChanged(s: Editable?) {
                numTxt.text = "${replyEdit.text.length}/60"
                if(numTxt.text.isNotEmpty()) {
                    sendTxt.isClickable = true
                    sendTxt.setTextColor(Color.parseColor("#ff7400"))
                } else {
                    sendTxt.isClickable = false
                    sendTxt.setTextColor(Color.parseColor("#999999"))
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })
        sendTxt.setOnClickListener {
            onSendClick?.invoke(replyEdit.text.toString())
            dismiss()
        }
    }

    private fun showKeyboard(editText: EditText) {
        editText.isFocusableInTouchMode = true
        editText.requestFocus()
        val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED)
    }

    private fun hideKeyboard(editText: EditText) {
        editText.requestFocus()
        val inputMethodManager = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(editText.windowToken, 0)
        editText.clearFocus()
    }
}
